import com.sun.source.tree.Tree;

import java.util.List;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
/*         Scanner scanner = new Scanner(System.in);
        String s = "1";
        String t = "2";
        System.out.println(solve(s,t)); */

        ListNode head1 = new ListNode(9);
        head1.next = new ListNode(3);
        ListNode tmp1 = head1;
        tmp1 = head1.next;
        tmp1.next = new ListNode(7);
        ListNode head2 = new ListNode(6);
        head2.next = new ListNode(3);

        ListNode ret = addInList(head1,head2);
    }

    public static String solve (String s, String t) {
        // write code here
        long a = (long) Integer.parseInt(s);
        long b = (long) Integer.parseInt(t);
        Long sum = a + b;
        return sum.toString();
    }

    public static ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        if(head1 == null){
            return head2;
        }

        if(head2 == null){
            return head1;
        }
        ListNode ret = new ListNode(-1);
        int len1 = length(head1);
        int len2 = length(head2);

        if(len2 > len1){
            ListNode tmp = head1;
            head1 = head2;
            head2 = tmp;
        }

        int diff = Math.abs(len1 - len2);
        ListNode cur1 = head1;
        ListNode cur2 = head2;
        ListNode cur = ret;
        for (int i = 0; i < diff; i++) {
            int val = cur1.val;
            ListNode node = new ListNode(val);
            cur.next = node;
            cur1 = cur1.next;
            cur = cur.next;
        }
        while (cur1 != null) {
            int sum = cur1.val + cur2.val;
            ListNode node = new ListNode(sum);
            cur.next = node;
            cur = cur.next;
            cur1 = cur1.next;
            cur2 = cur2.next;
        }

        ListNode prev = ret.next;
        cur = prev.next;
        while(cur != null){
            if(cur.val >= 10){
                cur.val -= 10;
                if(prev == ret){
                    ListNode tmp = new ListNode(1);
                    prev.next = tmp;
                    tmp.next = cur;
                    prev = prev.next;
                }else {
                    prev.val += 1;
                    cur = prev;
                    prev = ret;
                }
                while (prev.next != cur){
                    prev = prev.next;
                }
            }else {
                prev = prev.next;
                cur = cur.next;
            }
        }

        return ret.next;



    }

    public static int length(ListNode head){
        if(head == null){
            return 0;
        }

        int count = 0;
        ListNode cur = head;
        while (cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }
}
